package cn.edu.njtc.controller;

import cn.edu.njtc.common.R;
import cn.edu.njtc.entity.dto.MedicalVisitAddDto;
import cn.edu.njtc.entity.dto.MedicalVisitUpdateDto;
import cn.edu.njtc.entity.pojo.MedicalVisit;
import cn.edu.njtc.entity.pojo.User;
import cn.edu.njtc.exception.BizException;
import cn.edu.njtc.service.MedicalVisitService;
import cn.edu.njtc.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

@Tag(name = "就诊记录管理", description = "就诊记录管理接口")
@RestController
@RequestMapping("/visits")
public class MedicalVisitController {

    @Autowired
    private MedicalVisitService service;
    private MedicalVisit medicalVisit;

    @Autowired
    public MedicalVisitController(MedicalVisitService service, MedicalVisit medicalVisit) {
        this.service = service;
        this.medicalVisit = medicalVisit;
    }

    @Operation(summary = "获取所有医疗就诊记录")
    @GetMapping
    public R<List<MedicalVisit>> list() {
        return R.ok(service.listAll());
    }

    @Operation(summary = "根据ID获取医疗就诊记录详情")
    @GetMapping("/{id}")
    public R<List<MedicalVisit>> get(@PathVariable Integer id) throws BizException {
        List<MedicalVisit> visits = service.getById(id);
        return R.ok(visits);
    }

    @Operation(summary = "添加新的医疗就诊记录")
    @PostMapping
    public R<String> add(@RequestBody MedicalVisitAddDto visit) throws BizException {
        BeanUtils.copyProperties(visit, medicalVisit);
        service.add(medicalVisit);
        return R.ok("添加成功");
    }

    @Operation(summary = "更新医疗就诊记录")
    @PutMapping
    public R<String> update(@RequestBody MedicalVisitUpdateDto visit) throws BizException {
        BeanUtils.copyProperties(visit, medicalVisit);
        service.update(medicalVisit);
        return R.ok("更新成功");

    }

    @Operation(summary = "根据ID删除医疗就诊记录")
    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable Integer id) {
        service.delete(id);
        return R.ok("删除成功");
    }

    @Operation(summary = "分页查询医疗就诊记录")
    @GetMapping("/page")
    public R<IPage<MedicalVisit>> page(@RequestParam Integer num, @RequestParam  Integer size) {
        return R.ok(service.pageList(num, size));
    }
}
